package cn.hh.wechatkit.pojo.message.imp.commumication;


import cn.hh.wechatkit.pojo.message.imp.Wx_BaseMsg;
import cn.hhchat.kit.xml.XmlUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.List;
import java.util.Map;

/**
 * Created this one by HMH on 2017/3/24.
 */

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor

public class Wx_ArticleMsg extends Wx_BaseMsg {

    private Long articleCount;
    private String Title;
    private String Description;
    private String Url;

    private List<Article> articles;

    public Wx_ArticleMsg(Map<String, String> map) {
        super(map);
    }

    public String MsgToXml() {
        //生成xml文档
        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("utf-8");
        Element root = document.addElement("xml");
        //构造 ToUserName
        Element ToUserName = root.addElement("ToUserName")
                .addText(XmlUtil.genCdataAttr(this.getToUserName()));
        //添加 FromUserName
        Element FromUserName = root.addElement("FromUserName")
                .addText(XmlUtil.genCdataAttr(this.getFromUserName()));
        //构造 CreateTime
        Element createTime = root.addElement("CreateTime")
                .addText(XmlUtil.genCdataAttr(String.valueOf(this.getCreateTime())));
        //构造 MsgType
        Element msgType = root.addElement("MsgType")
                .addText(XmlUtil.genCdataAttr("news"));

        //构造 图文
        Element articles = root.addElement("Articles");
        for(int i = 0 ;i <articleCount;i++) {
            Element item = articles.addElement("item");
            //构造 item
            item.addElement("Title").addText(this.articles.get(i).title);
            item.addElement("Description").addText(this.articles.get(i).description);
            item.addElement("PicUrl").addText(this.articles.get(i).picUrl);
            item.addElement("Url").addText(this.articles.get(i).url);
        }
        return document.asXML();
    }


    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    class Article{
        private String title;
        private String description;
        private String picUrl;
        private String getPicUrl;
        private String url;
    }

}
